<?php

namespace app\company\controller;

use app\company\model\DeliverTaskImg;
use app\company\model\StoreOrderProfit;
use app\company\validate\DeliverTaskValidate;
use app\mini\model\StoreOrderDetail;
use app\service\traits\Crud;
use app\company\model\DeliverTask as DeliverTaskModel;
use app\company\model\StoreOrder;
use app\company\model\Store;
use think\facade\Request;
use think\Response;

class DeliverTask extends Base
{
    private DeliverTaskModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new DeliverTaskModel();
        $this->validateClass = DeliverTaskValidate::class;
        $this->isGs          = true;
        $this->withSearch    = "id,no";
        $this->append        = 'task_status_text';
    }


    public function format($list): mixed
    {
        return $list->each(function ($info) {
            $info->deliver_car_no      = $info->deliverCar()->value('no');
            $info->deliver_driver_name = $info->deliverDriver()->value('name');
        });
    }

    public function detailFormat($info): mixed
    {
        $info->start_time          = setDates($info->start_time);
        $info->end_time            = setDates($info->end_time);
        $info->deliver_car_no      = $info->deliverCar()->value('no');
        $info->deliver_driver_name = $info->deliverDriver()->value('name');
        //配送收益
        $storeOrders  = $info->storeOrders()->column('id');
        $info->income = StoreOrderProfit::whereIn('store_order_id', $storeOrders)->sum('driver_profit');
        return $info;
    }


    public function orderList(): Response
    {
        $param           = request()->param();
        $page            = $param['page'] ?? 1;
        $limit           = $param['limit'] ?? 10;
        $deliver_task_id = $param['deliver_task_id'] ?? 0;
        // 路线信息
        $sql             = StoreOrder::withSearch('deliver_task_id,store_id,no', $param)->field('store_id')->group('store_id');
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $sql             = $sql->with(['store' => getWith('id,name')]);
        $count           = $sql->count();
        $list            = $sql->select()->each(function ($item) use ($deliver_task_id) {
            // 路线信息
            $line = DeliverTaskModel::where('id',$deliver_task_id)->with(['deliverLine'=>getWith('id,name,no')])->find();
            $arr = [
                'name' => '',
                'no'   => ''
            ];
            if($line){
                $arr = [
                    'name' => $line->deliverLine->name,
                    'no'   => $line->deliverLine->no
                ];
            }
            $toSql        = StoreOrder::where('store_id', $item->store_id)->where('deliver_task_id', $deliver_task_id);
            $storeId = $item->store_id;
            $item->detail = $toSql->field('id,no')->select()->each(function ($order) use ($arr,$deliver_task_id,$storeId) {
                $order->total_amount = $order->storeOrderDetails()->sum('amount'); // 订单金额
                $order->count  = $order->storeOrderDetails()->count();    //明细条数(品类数量)
                $order->detail = $order->storeOrderDetails()
                                       ->with([
                                           'goods'             => getWith('id,name,no'),
                                           'goodsRule'         => getWith('id,spec_name'),
                                           'storeOrderProfitO' => getWith('id,store_order_detail_id,driver_profit')
                                       ])
                                       ->field('id,money,amount,goods_id,goods_rule_id,quantity')
                                       ->select()->each(function ($vo) use ($arr,$deliver_task_id,$storeId) {
                                           $vo->line_name = $arr['name'];
                                           $vo->line_no   = $arr['no'];
                                           $vo->imgs  = DeliverTaskImg::where('deliver_task_id',$deliver_task_id)->where('store_id',$storeId)->field('id,img')->select();
                    });
            });
        });
        return $this->success('请求成功', compact('list', 'count'));
    }
}